package cn.newtouch.org.controller;

import cn.newtouch.basic.annotation.PreAuthorize;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.org.domain.Employee;
import cn.newtouch.org.query.EmployeeQuery;
import cn.newtouch.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有员工" )
    @PreAuthorize(name = "所有员工",sn = "employee:getAll")
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    /**
     * 接单
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "接单" )
    public List<Employee> accept(@PathVariable("shopId") Long shopId){

            return employeeService.accept(shopId);
    }

    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "通过ID查询员工",sn = "employee:get")
    public Employee findOne(@PathVariable Long id){
       return employeeService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "通过ID删除员工",sn = "employee:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    @PreAuthorize(name = "保存员工",sn = "employee:save")
    public JsonResult updateOrAdd(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                //添加操作
                employeeService.save(employee);
            }else{
                //修改操作
                employeeService.update(employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "分页查询员工",sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        try {
            return employeeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "批量删除员工",sn = "employee:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }


}
